package com.seuic.hnapps.util;

import android.text.TextUtils;
import android.util.Base64;

import com.seuic.hnapps.util.HexUtil;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Utils {

    public static String getField55(String[] tags, String orgF55){
        byte len = 0;
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<tags.length;i++) {
            sb.append(tags[i]);

            String value = Utils.getValueFromIccData(tags[i],orgF55);
            if(TextUtils.isEmpty(value)){
                if(tags[i].equals("9F37")){
                    value = "00000000";
                    len = 4;
                }
                else if(tags[i].equals("95")){
                    value = "0000000000";
                    len = 5;
                }
                else if(tags[i].equals("9F03")){
                    value = "0000000000";
                    len = 6;
                }
                else{
                    i++;
                    continue;
                }
            }

            len = (byte) (value.length()/2);

            if(tags[i].equals("9F33")){
                if (value.equals("000000")){
                    value = "E0E1C8";
                }
                len = 3;
            }

            if(len <= 127){

                byte[] bLen = new byte[1];
                bLen[0] = len;
                String lenStr = HexUtil.byteToHexString(bLen);
                sb.append(lenStr);
            }
            if(len > 127 && len<=255){

                byte[] bLen = new byte[2];
                bLen[0] = (byte) 0x81;
                bLen[1] = len;
                String lenStr = HexUtil.byteToHexString(bLen);
                sb.append(lenStr);
            }
            if(len > 255){
                byte[] bLen = new byte[3];
                bLen[0] = (byte) 0x82;
                bLen[1] = (byte) (len/256);
                bLen[2] = (byte) (len%256);
                String lenStr = HexUtil.byteToHexString(bLen);
                sb.append(lenStr);
            }
            sb.append(value);
        }
        return sb.toString();
    }

    /**
     * 从IC卡数据中提取数据
     * @param tag
     * @param data
     * @return
     */
    public static String getValueFromIccData(String tag,String data){
        int i=0;
        int len;
        byte[] value = null;
        byte[] field55 =  HexUtil.hexStringToByteArray(data);

        if(TextUtils.isEmpty(data)) return null;
        if(TextUtils.isEmpty(tag)) return null;

        while(i<field55.length){
            String subTag;
            if((field55[i] & 0x1f) == 0x1f)//130825 0x0f-->0x1f
            {
                byte[] bTag = new byte[2];
                bTag[0] = field55[i];
                bTag[1] = field55[i+1];
                subTag = HexUtil.byteToHexString(bTag);
                i += 2;
            }
            else
            {
                byte[] bTag = new byte[1];
                bTag[0] = field55[i];
                subTag = HexUtil.byteToHexString(bTag);
                i += 1;
            }
            if((field55[i] & 0x80) > 0) i++;
            len = field55[i];

            if(tag.equalsIgnoreCase(subTag))
            {
                value = new byte[len];
                System.arraycopy(field55, i+1, value, 0, len);
                break;
            }
            i = i +1 +len;
        }

        if (value != null && value.length != 0) {
            return HexUtil.byteToHexString(value);
        }
        return null;
    }

    /**
     * IC卡获取的2磁道信息转化成2磁道
     * @param field57
     * @return
     */
    public static String formatField57(String field57){
        if (TextUtils.isEmpty(field57)) return null;
        field57 = field57.toUpperCase();
        String track2=null;
        if (field57.substring(field57.length()-1).equalsIgnoreCase("F")){
            track2 = field57.substring(0,field57.length()-1);
        }else {
            track2 = field57;
        }
        return track2.replace("D", "=");
    }

    public static String xor(String data1, String data2) throws Exception {
        if (data1.length() != data2.length()) {
            throw new Exception("data1和data2数据长度必须相同");
        }
        byte[] bData1 = HexUtil.hexStringToByteArray(data1);
        byte[] bData2 = HexUtil.hexStringToByteArray(data2);
        byte[] out = new byte[bData1.length];
        for (int i=0;i<bData1.length;i++) {
            out[i] = (byte) (bData1[i] ^ bData2[i]);
        }
        return HexUtil.byteToHexString(out);
    }
}
